package GongNeng;

import GJ.ArrayList;
import GJ.Time;
import Main.Main;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

public class Borrow extends JFrame {

    private JLabel label;
    private JLabel label2;
    private JLabel label3;
    private JButton button;
    private JButton button2;
    private JPanel panel;
    private JTextField textField;
    private JTextField textField2;
    private String string2 = null;
    private int string6;
    private int string7;
    private int string8;
    private int string9;
    private int string10;
    private int sumMoney;
    private boolean flag1,flag2;

    public static void main(String args[]){
        Borrow borrow = new Borrow();
        borrow.setVisible(true);
    }

    public Borrow(){
        super();
        setTitle("图书借阅");
        setBounds(350,100,1200,800);
        getContentPane().setLayout(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel = new JPanel();
        panel.setBounds(0,0,1200,800);
        panel.setLayout(null);
        getContentPane().add(panel);

        label2 = new JLabel("图书借阅");
        label2.setBounds(530,100,200,50);
        label2.setFont(new Font("",Font.BOLD,30));
        panel.add(label2);

        label = new JLabel("借阅人姓名：");
        label.setBounds(360,200,160,50);
        label.setFont(new Font("",Font.BOLD,25));
        panel.add(label);

        label3 = new JLabel("借阅图书名:");
        label3.setBounds(360,280,150,50);
        label3.setFont(new Font("",Font.BOLD,25));
        panel.add(label3);

        textField = new JTextField();
        textField.setBounds(500,210,300,40);
        panel.add(textField);

        textField2 = new JTextField();
        textField2.setBounds(500,285,300,40);
        panel.add(textField2);

        button = new JButton("确认");
        button.setBounds(500,350,100,50);
        panel.add(button);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Time time = new Time();
                String str1 ="借阅人："+textField.getText()+";";
                String str2 ="借阅图书："+textField2.getText()+";";
                String str3 ="借阅时间："+time.toString()+";";
                String str4 ="书名："+textField2.getText();
                File file = new File("D://BorrowedBooks");
                BufferedWriter bufferedWriter = null;
                try {
                    bufferedWriter = new BufferedWriter(new FileWriter(file, true));
                    ArrayList arrayList2 = new ArrayList(100);
                    arrayList2.addBooks(str1);
                    arrayList2.addBooks(str2);
                    arrayList2.addBooks(str3);
                    for (int i = 0; i < arrayList2.size; i++) {
                        String[] song1 = new String[100];
                        song1[i] = arrayList2.get(i);
                        bufferedWriter.write(song1[i]);
                        bufferedWriter.flush();
                        flag1 = true;
                    }
                    bufferedWriter.newLine();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                try {
                    bufferedWriter.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                    if (flag1){
                        int minute2 = 0;
                        int hour2 = 0;
                        int second = time.returnSecond();
                        int minute = time.returnMinute();
                        int hour = time.returnHh();
                        int day = time.returnDay();
                        int month = time.returnMonth();
                        int year = time.returnYear();
                        int sum = second+30;
                        if (sum>=60){
                             minute2 = minute+1;
                             second = 0;
                             JOptionPane.showMessageDialog(null,"借阅成功！"+"归还时间为："+year+"年"+month+"月"+day+"日"+hour+"时"+minute2+"分"+second+"秒");
                             dispose();
                             new Borrow().setVisible(true);
                        }else if (minute>=59){
                             hour2 = hour+1;
                             second = -1;
                             hour = 0;
                            JOptionPane.showMessageDialog(null,"借阅成功！"+"归还时间为："+year+"年"+month+"月"+day+"日"+hour2+"时"+minute2+"分"+sum+"秒");
                            dispose();
                            new Borrow().setVisible(true);
                        }else{
                            JOptionPane.showMessageDialog(null,"借阅成功！"+"归还时间为："+year+"年"+month+"月"+day+"日"+hour+"时"+minute+"分"+sum+"秒");
                            dispose();
                            new Borrow().setVisible(true);
                        }

                    }

                    /*
                        File file2 = new File("D://Books");
                        try {
                            BufferedReader bufferedReader = new BufferedReader(new FileReader(file2));
                            ArrayList arrayList = new ArrayList(100);
                            String line;
                            String[] song;
                            String str5;//读出来的图书书序
                            String zhen = null;
                            while ((line = bufferedReader.readLine())!= null) {
                                song = line.split(";");
                                for (int i=0;i<song.length;i++){
                                    arrayList.addBooks(song[i]);
                                }
                                zhen = arrayList.get(1);
                                if ( zhen.equals(str4)) {
                                    str5 = arrayList.get(0);
                                    //String str6 = arrayList.get(1);
                                    //String str7 = arrayList.get(2);
                                    String str8 = arrayList.get(3);
                                    String str9 = str5+ "(在借中)";
                                    FileWriter fileWriter = new FileWriter(file2,true);
                                    PrintWriter pw = new PrintWriter(fileWriter);
                                    pw.println(str9);
                                    fileWriter.flush();
                                    pw.close();
                                    fileWriter.close();
                                    break;
                                }else{
                                    flag1 = false;
                                    break;
                                }
                            }
                        } catch (FileNotFoundException ex) {
                            ex.printStackTrace();
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }

                     */

            }
        });


        button2 = new JButton("退出");
        button2.setBounds(700,350,100,50);
        panel.add(button2);
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String buttonName = e.getActionCommand();
                if (buttonName.equals("退出")){
                    dispose();
                    new Main().setVisible(true);
                }
            }
        });
    }

    public int returnMoney() throws IOException {

        Time time = new Time();
        int str5= time.returnSecond();
        int str8 =time.returnMinute();
        int str9 = time.returnHh();
        int str10 = time.returnDay();
        int str11 = time.returnMonth();
        if (str10>string9){
            sumMoney = ((str10-string9)*24*3600)+(str9-string8)*3600+(str8-string7)*60+(str5-string6);
        }else if (str9>string8){
            sumMoney = (str9-string8)*3600+(str8-string7)*60+(str5-string6);
        }else if (str8>string7){
            sumMoney = (str8-string7)*60+(str5-string6);
        }else{
            sumMoney = (str5-string6);
        }
        return sumMoney*2;
    }
}
